package com.sk.bilibili.model.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;

import androidx.fragment.app.Fragment;

import com.sk.api.annotations.Autowired;
import com.sk.bilibili.R;
import com.sk.bilibili.callBack.RequestCallBack;
import com.sk.bilibili.mapper.AndroidMapper;
import com.sk.bilibili.mapper.MemberMapper;
import com.sk.bilibili.mapper.QrCodeMapper;
import com.sk.bilibili.model.BaseModel;
import com.sk.bilibili.other.BiliBiliProperty;
import com.sk.bilibili.utils.IntentUtils;
import com.sk.bilibili.utils.PreferenceUtils;
import com.sk.bilibili.utils.RequestUtils;
import com.sk.bilibili.view.activity.MainActivity;
import com.sk.bilibili.view.fragment.main.CollectFragment;
import com.sk.bilibili.view.fragment.main.HistoryFragment;
import com.sk.bilibili.view.fragment.main.HomeFragment;
import com.sk.bilibili.view.fragment.main.LaterFragment;
import com.sk.bilibili.view.fragment.main.SaveFragment;
import com.sk.bilibili.view.fragment.main.home.ChannelFragment;
import com.sk.bilibili.view.fragment.main.home.DynamicFragment;
import com.sk.bilibili.view.fragment.main.home.ShopFragment;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainModel extends BaseModel<MainActivity> {

    @Autowired
    MemberMapper memberMapper;
    @Autowired
    QrCodeMapper qrCodeMapper;
    @Autowired
    AndroidMapper androidMapper;
    @Autowired
    BiliBiliProperty property;
    @Autowired
    PreferenceUtils preferenceUtils;
    private static final Pattern SIGNATURE_PATTERN=Pattern.compile(".+\\?signature=(\\w+)");

    @Override
    public void loadData() {
        RequestUtils.process(memberMapper.getMember(null), member -> view.loadSuccess(member));
    }

    public void handleScanRes(Intent data) {
        String content = data.getStringExtra("content");
        Matcher matcher = SIGNATURE_PATTERN.matcher(content);
        if(matcher.matches()){//处理登录扫码
            RequestUtils.process(qrCodeMapper.scan(matcher.group(1)), new RequestCallBack<Void>() {
                @Override
                public void success(Void unused) {
                    view.scanSuccess();
                }
                @Override
                public void error(Throwable throwable) {
                    view.scanError(throwable.getMessage());
                }
            });
        }else {//处理普通扫码请求
            IntentUtils.startUrl(view,content);
        }
    }

    public void checkNew() {
        RequestUtils.process(androidMapper.getApp(), url -> {
            Integer newVersion = Integer.parseInt(url.substring(url.lastIndexOf('=') + 1));
            int nowVersion = property.getInt("version");
            if (nowVersion < newVersion) {
                view.isOld(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), newVersion);
            }
        });
    }

    public Fragment[] createFragment() {
        Fragment[] fragments = new Fragment[5];
        fragments[0] = new HomeFragment();
        fragments[1] = new HistoryFragment();
        fragments[2] = new SaveFragment();
        fragments[3] = new CollectFragment();
        fragments[4] = new LaterFragment();
        return fragments;
    }

    public void showLogout() {
        new AlertDialog.Builder(view)
                .setMessage("暂时设置只有退出登录功能,是否退出登录?")
                .setPositiveButton("我手滑了", null)
                .setNegativeButton("退出登录", (dialog, which) -> {
                    RequestUtils.process(memberMapper.logout());//只需要注销服务器数据即可 id可以复用
                    preferenceUtils.remove("username");
                    preferenceUtils.remove("password");
                    view.onClickLogout();
                }).show();
    }

    public void changeThem() {
        int them = preferenceUtils.getInt("theme", R.style.Theme_BiliBiliDay);
        boolean night=them==R.style.Theme_BiliBiliDay;
        if(night){
            them=R.style.Theme_BiliBiliNight;
        }else {
            them=R.style.Theme_BiliBiliDay;
        }
        preferenceUtils.setInt("theme",them);
        new AlertDialog.Builder(view)
                .setMessage(String.format("已经设置为%s,重启生效",night?"夜间模式":"日间模式"))
                .setPositiveButton("稍后重启", null)
                .setNegativeButton("立即重启", (dialog, which) -> view.reboot()).show();
    }

}